<?php
/**
 * Created by yb.
 * Author: 1021948372@qq.com
 * Date: 2020/7/7 07:07
 */
namespace app\admin\controller;

use app\common\controller\Base;
use think\captcha\Captcha;
class Login extends Base{

    public function login(){
        if(request()->isPost()){
            if(empty(input('username'))){
                $this->error('用户名不能为空');
            }
            if(empty(input('password'))){
                $this->error('密码不能为空,且只是6个字符');
            }
            if(empty(input('verify_code'))){
                $this->error('验证码不能为空');
            }
            if(!captcha_check(input('verify_code'))){
                $this->error('验证码错误');
            }

            $map = array(
                'module' => request()->module(),
                'username' => input('username'),
                'password' => md5(input('password')),
                'status' => 1
            );
            $adminData = db('admin')->where($map)->find();
            if($adminData){
                session(request()->module() . '_adminInfo',$adminData);
                $this->success('登录成功');
            }else{
                $this->error('用户名或密码不存在');
            }
        }else{
            $this->view->engine->layout(false);
            return $this->fetch();
        }
    }

    public function logout(){
        session(request()->module() . '_adminInfo', null);
        $this->success('退出成功');
    }

    public function verifyCode()
    {
        $captcha = new Captcha(array('length' => 4));
        return $captcha->entry();
    }

}